package com.sundaying.gooway.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.sundaying.gooway.dao.UrlClassDao;
import com.sundaying.gooway.dao.UrlItemDao;
import com.sundaying.gooway.entity.UrlClass;
import com.sundaying.gooway.entity.UrlItem;
import com.sundaying.gooway.vo.ClassUrlItem;

@Component
public class UrlItemService {
	private UrlItemDao urlItemDao;
	private UrlClassDao urlClassDao;

	public UrlItemDao getUrlItemDao() {
		return urlItemDao;
	}

	@Resource
	public void setUrlItemDao(UrlItemDao UrlItemDao) {
		this.urlItemDao = UrlItemDao;
	}

	public UrlClassDao getUrlClassDao() {
		return urlClassDao;
	}

	@Resource
	public void setUrlClassDao(UrlClassDao urlClassDao) {
		this.urlClassDao = urlClassDao;
	}

	/**
	 * 添加
	 * 
	 * @param urlItem
	 *            URL标签对象
	 * @return 是否成功
	 */
	public int add(UrlItem urlItem) {
		return urlItemDao.save(urlItem);
	}

	/**
	 * 更新URL标签
	 * 
	 * @param UrlItem
	 * @return
	 */
	public int update(UrlItem UrlItem) {
		return urlItemDao.update(UrlItem);
	}

	/**
	 * 删除URL标签对象
	 * 
	 * @param id
	 *            主键
	 * @return
	 */
	public int delete(long id) {
		return urlItemDao.delete(urlItemDao.load(id));
	}

	/**
	 * 获取所有URL标签
	 * 
	 * @return
	 */
	public List<UrlItem> list() {
		return urlItemDao.list();
	}

	/**
	 * 获取按某字段排序后的第n页共n行的URL标签集
	 * 
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param urlItem
	 *            查询条件
	 * @return
	 */
	public List<UrlItem> list(int page, int rows, String sort, String order,
			UrlItem urlItem) {
		String hql = "from UrlItem urlItem where 1=1 ";
		List<Object> param = new ArrayList<Object>();
		hql = addWhere(hql, urlItem, param);
		// 排序
		if(!sort.equals("")){
			hql += " order by " + sort;
			
			if(!order.toLowerCase().equals("asc")){
				hql += " desc";
			}
		}

		List<UrlItem> urlItemList = urlItemDao.list(hql, param, page, rows);
		return urlItemList;
	}

	public long total(UrlItem urlItem) {
		long result = 0;
		String hql = "select count(*) from UrlItem urlItem where 1=1 ";
		List<Object> param = new ArrayList<Object>();
		hql = addWhere(hql, urlItem, param);
		result = urlItemDao.count(hql, param);
		return result;
	}

	public List<UrlItem> getTopListByClassId(int classId, int rows) {
		String hql = "from UrlItem urlItem where 1=1";
		List<Object> param = new ArrayList<Object>();
		if (classId > 0) {
			hql += " and urlItem.classId=? ";
			param.add(classId);
		}
		List<UrlItem> urlItemList = urlItemDao.list(hql, param);
		return urlItemList;
	}

	public List<ClassUrlItem> getTopListMap() {
		List<UrlClass> urlClassList = urlClassDao.list();
		List<ClassUrlItem> classUrlItemList = new ArrayList<ClassUrlItem>();
		String hql = "from UrlItem a where 1=1";
		for (UrlClass urlClass : urlClassList) {
			List<Object> param = new ArrayList<Object>();
			if (urlClass.getId() > 0) {
				hql += " and urlItem.classId=? ";
				param.add(urlClass.getId());
			}
			List<UrlItem> urlItemList = urlItemDao.list(hql, param);
			ClassUrlItem classUrlItem = new ClassUrlItem();
			classUrlItem.setUrlClass(urlClass);
			classUrlItem.setUrlItemList(urlItemList);
			classUrlItemList.add(classUrlItem);
		}
		return classUrlItemList;
	}

	/**
	 * 
	 * @param hql
	 * @param urlItem
	 * @param param
	 * @return
	 */
	private String addWhere(String hql, UrlItem urlItem, List<Object> param) {
		if (urlItem.getName() != null && !urlItem.getName().trim().equals("")) {
			hql += " and urlItem.name like '%%" + urlItem.getName().trim()
					+ "%%' ";
		}
		if (urlItem.getClassId() > 0) {
			hql += " and urlItem.classId=? ";
			param.add(urlItem.getClassId());
		}
		return hql;
	}
}
